﻿using DotNetty.Transport.Channels;
using MedianSocketService.services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MedianSocketService.netty.HighPowerProtocol
{
    public class HighPowerHandler : ChannelHandlerAdapter
    {
        SocketClientInfo sc;
        public Action<SocketClientInfo> OnConnectedCompletedDelegate;
        public Action<SocketClientInfo> OnDisConnectedDelegate;
        public Action<SocketClientInfo, byte[]> OnReceiveCompletedDelegate;

        private HighPowerProtocolService highPowerProtocolService;
        /// <summary>
        /// 通道激活
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelActive(IChannelHandlerContext context)
        {
            // 获取远程地址
            IPAddress remoteAddr = ((IPEndPoint)context.Channel.RemoteAddress).Address;
            int remotePort = ((IPEndPoint)context.Channel.RemoteAddress).Port;
            ChannelManager channelManager = ChannelManager.GetInstance();
            sc = new SocketClientInfo() { Ip = remoteAddr.MapToIPv4().ToString(), Port = remotePort };
            //加入到通道管理中
            channelManager.AddChannel(sc, context.Channel);
            //中位机协议
            highPowerProtocolService = new HighPowerProtocolService();
            highPowerProtocolService.sci = sc;

            //通知连接成功
            if (OnConnectedCompletedDelegate != null)
            {
                OnConnectedCompletedDelegate(sc);
            }
            base.ChannelActive(context);
        }
        /// <summary>
        /// 失去连接
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelInactive(IChannelHandlerContext context)
        {
            ChannelManager channelManager = ChannelManager.GetInstance();
            channelManager.RemoveChannel(sc);
            if (OnDisConnectedDelegate != null)
            {
                OnDisConnectedDelegate(sc);
            }
            base.ChannelInactive(context);
        }
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            byte[] resultByte = (byte[])message;
            //通知
            if (OnReceiveCompletedDelegate != null)
            {
                OnReceiveCompletedDelegate(sc, resultByte);
            }
            //解包
            highPowerProtocolService.UnPack(resultByte);
        }
        /// <summary>
        /// 读取完成
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelReadComplete(IChannelHandlerContext context)
        {
            base.ChannelReadComplete(context);
        }
        /// <summary>
        /// 发生异常
        /// </summary>
        /// <param name="context"></param>
        /// <param name="exception"></param>
        public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
        {
        }
    }
}